<?
/**
**江西瑞金
**2015-05-29
**User by pzp
**/
class RunJIN{
	private $USER = 'PFTORDER';
	private $PWD = 'PFTORDER';
	// private $submit_url = 'http://121.42.56.222/et/ebusinessTC/submitOrder.do';
	private $submit_url = 'jtsvn.eicp.net:3321/et/ebusinessTC/submitOrder.do';
	private $select_url = 'http://121.42.56.222/et/ebusinessTC/orderInfo.do';
	private $remove_url = 'http://121.42.56.222/et/ebusinessTC/removeOrder.do';
	public function __construct($lid){
		switch($lid){
			case 8054:  //红井
				$this->BARCODES= 'HJPFT';
				$this->ticket_url = 'http://61.180.70.105:81/et/ebusinessTC/ticketInfo.do';
			break;
			case 8055:  //二苏大
				$this->BARCODES= 'ESPFT';
				$this->ticket_url = 'http://61.180.70.102:81/et/ebusinessTC/ticketInfo.do';
			break;
			case 8048:  //叶坪
				$this->BARCODES= 'YPPFT';
				$this->ticket_url = 'http://61.180.70.99:81/et/ebusinessTC/ticketInfo.do';
			break;
		}
	}
	
	//票类查询
	public function ticketInfo(){
		$post_array = array();
		$ticketInfo_array = $this->RJ_POST($post_array,$this->ticket_url);
		return $ticketInfo_array;
	} 
	
	//订单提交
	public function submitOrder(){
		$time = time();
		// for($i=0;$i<$request_array['UUtnum'];$i++){
			// $BARCODE = $this->BARCODES.$time.$i;
			// $BARCODEs[].= $BARCODE;
		// }		
		for($i=0;$i<2;$i++){
			$BARCODE = $this->BARCODES.$time.$i;
			$BARCODEs[].= $BARCODE;
		}	
		$BARCODEss = json_encode($BARCODEs);
		/*
		$post_array = array(
			'ORDERID'=>$request_array['pftordernum'], //电商自己的订单号
			'TICKETORDERID'=>$request_array['pftordernum'], //电商票类订单号
			'NUMBER'=>$request_array['IDcar'], //取票人身份证
			'NAME'=>$request_array['pftordername'], //取票人姓名
			'MOBILE'=>$request_array['UUordertel'], //取票手机号码
			'CUSTOMERNAME'=>'票付通', //客户名称（旅行社名称）
			'SOURCE'=>'票付通', //客源地
			'BARCODES'=>$BARCODEss, //条码集合
			'COUNT'=>$request_array['UUtnum'], //人数
			'PRICE'=>$request_array['UUlprice'], //单价
			'AMOUNT'=>$request_array['UUtnum'] * $request_array['UUlprice'], //总金额
			'BEGINDATE'=>date('Ymd',strtotime($request_array['UUordertime'])), //有效期(起始)格式：yyyyMMddHHmmss
			'ENDDATE'=>str_replace('-','',$request_array['UUendtime']), //有效期(截止)
			'TICKETUUID'=>$request_array['uuid'], //票类编号
			'TYPE'=>1, //是否一票多人
			'NETWORK'=>'票付通',  //预定网站名称
			'ORDER_STATE'=>0, //订单状态
			'ORDERDATE'=>date('YmdHis',strtotime($request_array['UUordertime'])), //订单时间
			'ORDER_TYPE'=>'票付通', //订单类型
			'VIEWNAME'=>$request_array['UUltitle'], //景区名称
			'TICKETNAME'=>$request_array['productname'], //票类名称
			'OPERATERNAME'=>$this->USER, //操作员名称
			'STATE'=>$request_array['ispay'],
			'QRCODE'=>$request_array['UUcode']
		);
		*/
		$post_array = array(
			'ORDERID'=>"20150604002", //电商自己的订单号
			'TICKETORDERID'=>"20150604002", //电商票类订单号
			'NUMBER'=>"350321199108230123", //取票人身份证
			'NAME'=>"彭志萍", //取票人姓名
			'MOBILE'=>"15959110796", //取票手机号码
			'CUSTOMERNAME'=>'票付通', //客户名称（旅行社名称）
			'SOURCE'=>'票付通', //客源地
			'BARCODES'=>$BARCODEss, //条码集合
			'COUNT'=>2, //人数
			'PRICE'=>"35", //单价
			'AMOUNT'=>"70", //总金额
			'BEGINDATE'=>date('Ymd',strtotime("2015-06-04 17:08:08")), //有效期(起始)格式：yyyyMMddHHmmss
			'ENDDATE'=>"20150608", //有效期(截止)
			'TICKETUUID'=>"4649356AC4494B119366C1FC91D5117C", //票类编号
			'TYPE'=>1, //是否一票多人
			'NETWORK'=>'票付通',  //预定网站名称
			'ORDER_STATE'=>0, //订单状态
			'ORDERDATE'=>date('YmdHis',strtotime("2015-06-04 17:08:08")), //订单时间
			'ORDER_TYPE'=>'票付通', //订单类型
			'VIEWNAME'=>"叶坪", //景区名称
			'TICKETNAME'=>"全额票", //票类名称
			'OPERATERNAME'=>$this->USER, //操作员名称
			'STATE'=>"1",
			'QRCODE'=>"920903"
		);
		print_r($post_array);
		$submitOrder_array = $this->RJ_POST($post_array,$this->submit_url);
		print_r($submitOrder_array);exit;
		if($submitOrder_array['content']['code'] == 1){
			return 100;
		}else{
			$error_msg = $submitOrder_array['content']['message'];
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$request_array['pftordernum'].'|'.$error_msg.'|'.json_encode($post_array));
			return 101;
		}
	}
	
	//订单查询 
	public function orderInfo(){
		$post_array = array(
			'ORDERID'=>'0601001', //电商自己的订单号
			'TICKETORDERID'=>'601001', //电商票类订单号
		);
		print_r($post_array);
		$orderInfo_array = $this->RJ_POST($post_array,$this->select_url);
		return $orderInfo_array;			
	}
	
	//订单撤销
	public function removeOrder($order_true){
		$post_array = array(
			'ORDERID'=>$order_true, //电商自己的订单号
		);
		$removeOrder_array = $this->RJ_POST($post_array,$this->remove_url);
		if($removeOrder_array['content']['code'] == 1){
			return 100;
		}else{
			$error_msg = $removeOrder_array['content']['message'];
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$order_true.'|'.$error_msg);
			return 101;
		}
	}
	
	//post
	public function RJ_POST($post_array,$url){
		$o=""; 
		foreach ($post_array as $k=>$v){ 
			$o.= "$k=".urlencode($v)."&"; 
		} 
		$post_array=substr($o,0,-1); 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL,$url); 
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array); 
		$reponse = curl_exec($ch);
		curl_close($ch);
		if($reponse){
			return json_decode($reponse,true);			
		}else{
			return $reponse;
		}
	}
	
	//日志
	public function logfile($txt){
		$fp = fopen('/var/www/html/new/d/module/api/RuiJin/ruijin_logfile.txt',"a");
		flock($fp, LOCK_EX) ;
		fwrite($fp,$txt."\n");
		flock($fp, LOCK_UN);
		fclose($fp); 
	}
}
$lid = 8048;
$RJ = new RunJIN($lid);
// $get_ticketInfo = $RJ->ticketInfo();print_r($get_ticketInfo);
$submit_order = $RJ->submitOrder();print_r($submit_order);
// $orderInfo_order = $RJ->orderInfo();print_r($orderInfo_order);
// $removeOrder_order = $RJ->removeOrder();print_r($removeOrder_order);
 
?>